/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  BaseWebServiceAPI.java   
 * @Package com.jiyi.service.api   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-8-4 下午1:54:53   
 * @version V1.0     
 */  
package com.jiyi.service.api;

import java.io.File;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.GetBuilder;
import com.zhy.http.okhttp.builder.PostFormBuilder;
import com.zhy.http.okhttp.callback.StringCallback;

/**   
 * @ClassName:  BaseWebServiceAPI   
 * @Description:TODO(这里用一句话描述这个类的作用)   
 * @author: HYM  
 * @date:   2017-8-4 下午1:54:53   
 *      
 */
public class BaseWebServiceAPI {

	volatile static BaseWebServiceAPI instence;
	
	public static BaseWebServiceAPI getInstance() {
		if (null == instence) {
			synchronized (BaseWebServiceAPI.class) {
				if (null == instence) {
					instence = new BaseWebServiceAPI();
				}
			}
		}
		return instence;
	}
	
	
	public void reqeustUrl(String url, Map<String, String> map, String postType,StringCallback stringCallback) {
		if (postType.equals(PostType.POST)) {
			postWebUrl(url, map,stringCallback);
		} else {
			getWebUrl(url, map,stringCallback);
		}
	}

	private void getWebUrl(String url, Map<String, String> map,StringCallback stringCallback) {
		GetBuilder getBuilder = OkHttpUtils.get().url(url);
		if (map != null) {
			Set<Map.Entry<String, String>> sets = map.entrySet();
			for (Map.Entry<String, String> entry : sets) {
				String key = entry.getKey();
				String value = entry.getValue();
				getBuilder.addParams(key, value);
			}
		}
		getBuilder.build().execute(stringCallback);
	}

	private void postWebUrl(String url, Map<String, String> map,StringCallback stringCallback) {
		PostFormBuilder postFormBuilder = OkHttpUtils.post().url(url);
		if (map != null) {
			Set<Map.Entry<String, String>> sets = map.entrySet();
			for (Map.Entry<String, String> entry : sets) {
				String key = entry.getKey();
				String value = entry.getValue();
				postFormBuilder.addParams(key, value);
			}
		}
		postFormBuilder.build().execute(stringCallback);
	}
	
	public void postFile(String url,String fileKeyName,Map<String, File> fileMap,Map<String, String> map,StringCallback stringCallback)
	{
		PostFormBuilder postFormBuilder = OkHttpUtils.post();
		if (fileMap != null) {
//			postFormBuilder.addFiles(fileKeyName, fileMap);
			Set<Entry<String, File>> sets = fileMap.entrySet();
			for (Map.Entry<String, File> entry : sets) {
				String key = entry.getKey();
				File value = entry.getValue();
				postFormBuilder.addFile(key, key+".jpg", value);
			}
		}
		
		if (map != null) {
			Set<Map.Entry<String, String>> sets = map.entrySet();
			for (Map.Entry<String, String> entry : sets) {
				String key = entry.getKey();
				String value = entry.getValue();
				postFormBuilder.addParams(key, value);
			}
		}
		postFormBuilder.url(url).build().execute(stringCallback);
	}
	
}
